#include "Player.h"

Player::Player(QTcpSocket* socket)
    : socket(socket)
{
    QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
}

void Player::onReadyRead()
{
    auto msg = QString(socket->readAll());
    auto args = msg.split(' ');

    // 玩家设置名称
    if (args[0] == "NAME") {
        name = args.mid(1).join(' ');
        emit named(name);
    }
}

void Player::sendMessage(const QString& msg)
{
    socket->write(msg.toStdString().c_str());
    socket->waitForBytesWritten();
}

void Player::onDisconnected()
{
    emit disconnected();
}

const QString& Player::getName() const
{
    return name;
}
